﻿#include "opcua.h"

OpcUA::OpcUA()
{
    client = new Open62541();
}

bool OpcUA::OpcUA_Init(QString endpointUrl)
{
    return client->Open62541_Init(endpointUrl);
}


static QStringList valveID_OnlyRead = {
    "omron.nj101.阀门输出1",
    "omron.nj101.阀门输出2",
    "omron.nj101.阀门输出3",
    "omron.nj101.阀门输出4",
    "omron.nj101.阀门输出5",
    "omron.nj101.阀门输出6",
    "omron.nj101.阀门输出7",
    "omron.nj101.阀门输出8",
    "omron.nj101.阀门输出9",
    "omron.nj101.阀门输出10",
    "omron.nj101.阀11"
};

static QStringList valveID_ReadAndWrite = {
    "omron.nj101.氮气阀上位机",
    "omron.nj101.硫化氢1_32阀上位机",
    "omron.nj101.硫化氢1_52阀上位机",
    "omron.nj101.硫化氢2_32阀上位机",
    "omron.nj101.硫化氢2_52阀上位机",
    "omron.nj101.源瓶1line阀上位机",
    "omron.nj101.源瓶1Run阀上位机",
    "omron.nj101.源瓶2Line阀上位机",
    "omron.nj101.源瓶2Run阀上位机",
    "omron.nj101.换源气阀上位机"
};

static QStringList OtherValveID_OnlyRead = {
    "omron.nj101.规阀",
    "omron.nj101.新增真空泵",
    "omron.nj101.新增真空泵_MP启动",
    "omron.nj101.新增真空泵_BP启动",
    "omron.nj101.炉子开启",
    "omron.nj101.指示灯1",     //绿色指示灯
    "omron.nj101.指示灯",      //黄色指示灯
    "omron.nj101.指示灯2"      //红色指示灯
};

static QStringList flowmeterID_OnlyRead = {
    "omron.nj101.流量读数1",
    "omron.nj101.流量读数2",
    "omron.nj101.流量读数3",
    "omron.nj101.流量读数4",
    "omron.nj101.流量读数5",
    "omron.nj101.流量读数6",
    "omron.nj101.流量读数7",
    "omron.nj101.流量读数8",
    "omron.nj101.流量读数9"
};

static QStringList flowmeterID_ReadAndWrite = {
    "omron.nj101.流量1设定",
    "omron.nj101.流量2设定",
    "omron.nj101.流量3设定",
    "omron.nj101.流量4设定",
    "omron.nj101.流量5设定",
    "omron.nj101.流量6设定",
    "omron.nj101.流量7设定",
    "omron.nj101.流量8设定",
    "omron.nj101.流量9设定"
};

static QStringList pressuremeterID_OnlyRead = {
    "omron.nj101.压力读数1",
    "omron.nj101.压力读数2"
};

static QStringList pressuremeterID_ReadAndWrite = {
    "omron.nj101.压力设定1",
    "omron.nj101.压力设定2"
};

static QStringList MKSID_OnlyRead = {
    "omron.nj101.MKS反馈1",
    "omron.nj101.MKS反馈2"
};

static QStringList MKSID_ReadAndWrite = {
    "omron.nj101.MKS设定1",
    "omron.nj101.MKS设定2",
    "omron.nj101.MKS控制"
};

static QStringList temperatureID_OnlyRead = {
    "omron.nj101.实际温度1",
    "omron.nj101.实际温度2"
};

static QStringList temperatureID_ReadAndWrite = {
    "omron.nj101.温控启动",
    "omron.nj101.温度设定值动态",
    "omron.nj101.温度设定值动态1"
};

static QStringList OtherDataID_OnlyRead = {
    "omron.nj101.腔外壳体温度",
    "omron.nj101.压力实数9",
    "omron.nj101.水温1",
    "omron.nj101.水温2",
    "omron.nj101.水压",
    "omron.nj101.水流",
    "omron.nj101.Loop1PV",
    "omron.nj101.Loop2PV"
};

bool OpcUA::readValveStatus(int valveChannel, bool &result)
{
    if( valveChannel == 0 || valveChannel > valveID_OnlyRead.size() )
    {
        return false;
    }
    return client->Open62541_ReadData(2, valveID_OnlyRead[valveChannel - 1], result);
}

bool OpcUA::readAllValveStatus(QVector<bool> &result)
{
    QMap<QString, QVariant> data;
    bool myResult = client->Open62541_batchReading(2, valveID_OnlyRead, data);
    for(int i = 0; i < valveID_OnlyRead.size(); i++)
    {
        result[i] = data.value(valveID_OnlyRead[i]).toBool();
    }

    return myResult;
}


bool OpcUA::writeValveStatus(int valveChannel, bool status)
{
    if( valveChannel == 0 || valveChannel > valveID_ReadAndWrite.size() )
    {
        return false;
    }
    return client->Open62541_WriteData(2, valveID_ReadAndWrite[valveChannel - 1], status);
}

bool OpcUA::writeAllValveStatus(QVector<bool> status)
{
    int maxValue = qMin(status.size(), valveID_ReadAndWrite.size());
    for(int i = 0; i < maxValue; i++)
    {
        if (!client->Open62541_WriteData(2, valveID_ReadAndWrite[i], status[i]))
            return false;
    }
    return true;
}

bool OpcUA::readFlowRate(int flowmeterChannel, int16_t &result)
{
    if( flowmeterChannel == 0 || flowmeterChannel > flowmeterID_OnlyRead.size() )
    {
        return false;
    }
    return client->Open62541_ReadData(2, flowmeterID_OnlyRead[flowmeterChannel - 1], result);
}

bool OpcUA::readAllFlowRate(QVector<int16_t> &result)
{
    QMap<QString, QVariant> data;
    bool myResult = client->Open62541_batchReading(2, flowmeterID_OnlyRead, data);
    for(int i = 0; i < flowmeterID_OnlyRead.size(); i++)
    {
        result[i] = data.value(flowmeterID_OnlyRead[i]).toInt();
    }

    return myResult;
}

bool OpcUA::writeFlowRate(int flowmeterChannel, int16_t inputValue)
{
    if( flowmeterChannel == 0 || flowmeterChannel > flowmeterID_ReadAndWrite.size() )
    {
        return false;
    }
    return client->Open62541_WriteData(2, flowmeterID_ReadAndWrite[flowmeterChannel - 1], inputValue);
}

bool OpcUA::writeAllFlowRate(QVector<int16_t> inputValue)
{
    int maxValue = qMin(inputValue.size(), flowmeterID_ReadAndWrite.size() );
    for(int i = 0; i < maxValue; i++)
    {
        if( !client->Open62541_WriteData(2, flowmeterID_ReadAndWrite[i], inputValue.value(i)))
            return false;
    }
    return true;
}


bool OpcUA::readPressure(int pressuremeterChannel, int16_t &result)
{
    if( pressuremeterChannel == 0 || pressuremeterChannel > pressuremeterID_OnlyRead.size() )
    {
        return false;
    }
    return client->Open62541_ReadData(2, pressuremeterID_OnlyRead[pressuremeterChannel - 1], result);
}

bool OpcUA::readAllPressure(QVector<int16_t> &result)
{
    QMap<QString, QVariant> data;
    bool myResult = client->Open62541_batchReading(2, pressuremeterID_OnlyRead, data);
    for(int i = 0; i < pressuremeterID_OnlyRead.size(); i++)
    {
        result[i] = data.value(pressuremeterID_OnlyRead[i]).toInt();
    }
    return myResult;
}

bool OpcUA::writePressure(int pressuremeterChannel, int16_t inputValue)
{
    if( pressuremeterChannel == 0 || pressuremeterChannel > pressuremeterID_ReadAndWrite.size() )
    {
        return false;
    }
    return client->Open62541_WriteData(2, pressuremeterID_ReadAndWrite[pressuremeterChannel - 1], inputValue);
}

bool OpcUA::writeAllPressure(QVector<int16_t> inputValue)
{
    int maxValue = qMin( inputValue.size(), pressuremeterID_ReadAndWrite.size() );
    for(int i = 0; i < maxValue; i++)
    {
        if( !client->Open62541_WriteData(2, pressuremeterID_ReadAndWrite[i], inputValue.value(i)) )
            return false;
    }
    return true;
}

bool OpcUA::readMKS(int16_t &pressure, int16_t &openingAmplitude)
{
    return client->Open62541_ReadData(2, MKSID_OnlyRead[0], pressure)
            && client->Open62541_ReadData(2, MKSID_OnlyRead[1], openingAmplitude);
}

bool OpcUA::writeMKS(int16_t pressure, int16_t openingAmplitude, int16_t switchMode)
{
    if(MKSID_ReadAndWrite.size() < 3)
        return false;
    return client->Open62541_WriteData(2, MKSID_ReadAndWrite[0], pressure)
            && client->Open62541_WriteData(2, MKSID_ReadAndWrite[1], openingAmplitude)
            && client->Open62541_WriteData(2, MKSID_ReadAndWrite[2], switchMode);
}

bool OpcUA::readTemperature(int16_t &temperature1, int16_t &temperature2)
{
    return client->Open62541_WriteData(2, temperatureID_OnlyRead[0], temperature1)
            && client->Open62541_WriteData(2, temperatureID_OnlyRead[1], temperature2);
}

bool OpcUA::writeTemperature(bool status, int16_t temperature1, int16_t temperature2)
{
    if(temperatureID_ReadAndWrite.size() < 3)
        return false;
    return client->Open62541_WriteData(2, temperatureID_ReadAndWrite[0], status)
            && client->Open62541_WriteData(2, temperatureID_ReadAndWrite[1], temperature1)
            && client->Open62541_WriteData(2, temperatureID_ReadAndWrite[2], temperature2);
}

bool OpcUA::readOtherValveStatus(int valveChannel, bool &result)
{
    if( valveChannel == 0 || valveChannel > OtherValveID_OnlyRead.size() )
    {
        return false;
    }
    return client->Open62541_ReadData(2, OtherValveID_OnlyRead[valveChannel - 1], result);
}

bool OpcUA::readOtherDataValue(int dataChannel, int16_t &result)
{
    if( dataChannel == 0 || dataChannel > OtherDataID_OnlyRead.size() )
    {
        return false;
    }
    return client->Open62541_ReadData(2, OtherDataID_OnlyRead[dataChannel - 1], result);
}
